﻿using System;
using System.Collections.Generic;
using NHibernate.Criterion;
using Ornament.NHibernates;

namespace Products.Databases.DataAccess
{
    public class TableAccess : DaoBase<Guid, Table>
    {
        protected override Order DefaultOrder
        {
            get { return Order.Asc("ConceptName"); }
        }
        public Table Get(string conceptName)
        {
            var cri = CreateDetachedCriteria().Add(Restrictions.Eq("ConceptName", conceptName))
                .GetExecutableCriteria(this.CurrentSession);
            var result = cri.UniqueResult<Table>();
            return result;
        }
        public IList<Table> GetTables(Product product)
        {
            return CreateCriteria()
                .Add(Restrictions.Eq("Product", product))
                .AddOrder(DefaultOrder)
                .List<Table>();

        }

        public Table Get(FieldSnapshot field)
        {
            var cri = DetachedCriteria.For(typeof(Field)).
                CreateCriteria("Snapshots", "as").Add(Restrictions.Eq("as.Id", field.Id));
            return cri.GetExecutableCriteria(this.CurrentSession).UniqueResult<Field>().Table;

        }
    }
}